package com.infinitus.nginxInfo.config;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;



import com.infinitus.nginxInfo.task.FilterRules;

public class StaticCache {
	
	public static final Map<String,String> FILEDTYPE_MAP = new ConcurrentHashMap<String, String>();
	static{
		FILEDTYPE_MAP.put("0","l");
		FILEDTYPE_MAP.put("1","f");
		FILEDTYPE_MAP.put("2","s");
		FILEDTYPE_MAP.put("4","d");
		FILEDTYPE_MAP.put("5","i");
	}
	private static final Map<String,String> TOPIC_FILEDTYPE_MAP = new ConcurrentHashMap<String, String>();
	
	private static final Map<String, String> TOPICTOKENS = new ConcurrentHashMap<String, String>();
	
	
	public static void setTopicTokens(Map<String, String> topicTokens) {
		for(Entry<String, String> entry : topicTokens.entrySet()){
			String updateTopic = entry.getKey();
			String updateToken = entry.getValue();
			String cacheToken = TOPICTOKENS.get(updateTopic);
			if(updateToken!=null &&(cacheToken==null || !cacheToken.equals(updateToken))){
				TOPICTOKENS.put(updateTopic, updateToken);
			}
		}
	}
	
	public static String getTopicToken(String topic) {
		if(FilterRules.isNotBlank(topic)){
			return TOPICTOKENS.get(topic);
		}
		return null;
	}
	
	public static void setFieldType(String topicField, String type) {
		String updateStruct = FILEDTYPE_MAP.get(type);
		String cacheStruct = TOPIC_FILEDTYPE_MAP.get(topicField);
		if(updateStruct!=null &&(cacheStruct==null || !cacheStruct.equals(updateStruct))){
			TOPIC_FILEDTYPE_MAP.put(topicField, updateStruct);
		}
	}
	
	public static String getFiledType(String topicField) {
		if(FilterRules.isNotBlank(topicField)){
			return TOPIC_FILEDTYPE_MAP.get(topicField);
		}
		return null;
	}
	
}
